package cn.liccshop.core.service.shopUserAddress.controller;


import cn.liccshop.core.entity.ShopUserAddress;
import cn.liccshop.core.service.shopUserAddress.service.IShopUserAddressService;
import cn.liccshop.core.utils.ObjectToOtherUtils;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.exception.MySystemException;
import cn.liccshop.sys.utils.GaoDeUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import cn.liccshop.sys.base.MyActionController;

import java.util.List;
import java.util.Map;

/**
 * @description 用户收货地址表 前端控制器
 *
 * @author LiCC
 * @since 2019-05-06
 */
@RestController
@RequestMapping("/shopUserAddress")
public class ShopUserAddressController extends MyActionController<ShopUserAddress> {

    @Autowired
    private IShopUserAddressService liShopUserAddressService;

    @Override
    protected IService<ShopUserAddress> getLiService(){
        return liShopUserAddressService;
    }

    @RequestMapping(value = "/infoByKey")
    public Result getInfoByKey(@RequestBody Map<String, Object> map) throws MySystemException {
        String keywords = ObjectToOtherUtils.toString(map.get("keywords"));
        Integer subdistrict = ObjectToOtherUtils.toInteger(map.get("subdistrict"));
        JSONObject data = GaoDeUtil.getAddressInfo(keywords,subdistrict);
        return new Result().setMessage("成功获取信息").setData(data).setCode(Constants.OK);
    }
    @RequestMapping(value = "/save")
    public Result getInfoByKey(@RequestBody ShopUserAddress address) throws MySystemException {
        address.setUserId(getUserInfoByToken().getUserId());
        ShopUserAddress data = liShopUserAddressService.saveAddressInfo(address);
        return new Result().setMessage("信息保存成功").setData(data).setCode(Constants.OK);
    }

    @Override
    public Result dataList(@RequestBody ShopUserAddress entity) throws MySystemException {
        String userId = getUserInfoByToken().getUserId();
        if (StringUtils.isBlank(userId)){
            throw new MySystemException("您还未登陆，请登陆后在操作", Constants.UNAUTHEN);
        }
        IService<ShopUserAddress> service = this.theService();
        List<ShopUserAddress> data = service.list(new QueryWrapper<>(entity.setUserId(userId)).orderByDesc("create_date"));
        return new Result().setCode(Constants.OK).setData(data);
    }
}

